package com.pzl.hwr.util;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Created by zl.peng on 2017/6/19 15:46.
 */

public class Util {

    public static final String TAG = "Util";

    /**
     * 灵云离线资源存放目录
     */
    public static String HciCloudDataPath;

    public static int unit(Activity activity, int num) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels / num;
    }


    /**
     * application 启动时操作
     * 把灵云的离线资源拷贝到 app私有文件目录 data/data/appDir/files/中
     *
     * @param context
     * @param assetsFilePath
     */
    public static void copyHciCloudOfflineRes2AppFiles(Context context, ArrayList<String> assetsFilePath) {
        String dataPath = context.getFilesDir().getAbsolutePath() + File.separator
                + "HciCloud" + File.separator
                + "LocalRes" + File.separator;

        HciCloudDataPath = dataPath;

        for (String path : assetsFilePath) {
            copyAssetsToAppFiles(context, path, dataPath);
        }

    }


    /**
     * 把data/app/中assets文件拷贝到data/data/appDir/files/中
     *
     * @param context        上下文
     * @param assetsFilePath 文件相对路径
     * @param dataPath       app私有文件路径 files/
     */
    private static void copyAssetsToAppFiles(Context context, String assetsFilePath, String dataPath) {
        InputStream inputStream = null;
        FileOutputStream fos = null;
        try {
            File path = new File(dataPath);
            if (!path.exists()) {
                path.mkdirs();
            }
            Log.d(TAG, "assetsFilePath = " + assetsFilePath);
            Log.d(TAG, "outPath = " + dataPath);
            fos = new FileOutputStream(dataPath + getAssetsFileName(assetsFilePath));
            inputStream = context.getAssets().open(assetsFilePath);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static String getAssetsFileName(String path) {
        String temp[] = path.split("/");
        Log.d(TAG, "assetsFileName = " + temp[temp.length - 1]);
        return temp[temp.length - 1];
    }


    /**
     * 初始化灵云离线资源
     */
    public static void initHciCloudOfflineRes(Context context) {
        ArrayList<String> offlineAssets = new ArrayList<>();
        //手写识别 词组联想离线资源
        offlineAssets.add("associate/wa.system.dct");
        offlineAssets.add("associate/wa.user.dct");
        //手写识别 离线多字识别
        offlineAssets.add("freestylus/fwlib.dic");
        offlineAssets.add("freestylus/letter.conf");
        offlineAssets.add("freestylus/letter.dic");
        offlineAssets.add("freestylus/wwlib.dic");

        //offlineAssets.add("HciCloudSetting.txt");

        copyHciCloudOfflineRes2AppFiles(context, offlineAssets);
    }

}
